package com.kkd.kkdapp.activity;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.base.BaseActivity;
import com.kkd.kkdapp.fragment.TenderDetailFragment;

/**
 * 投标明细
 * 
 * @author mty
 */
public class TenderDetailActivity extends BaseActivity implements
		OnClickListener {
	private RelativeLayout intender; // 投标中
	private TextView intender_underline; // 投标中下划线
	private RelativeLayout refund; // 还款中
	private TextView refund_underline; // 还款中下划线
	private RelativeLayout refunded; // 已还款
	private TextView refunded_underline; // 已还款下划线
	private RelativeLayout td_top_img; // 返回

	private ViewPager mPager;
	private FragmentAdapter mAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_tender_detail);
		initView();
		setListener();
		// requestTenderDetail("5002");
	}

	public void initView() {
		intender = (RelativeLayout) this.findViewById(R.id.intender);
		intender_underline = (TextView) this
				.findViewById(R.id.intender_underline);
		refund = (RelativeLayout) this.findViewById(R.id.refund);
		refund_underline = (TextView) this.findViewById(R.id.refund_underline);
		refunded = (RelativeLayout) this.findViewById(R.id.refunded);
		refunded_underline = (TextView) this
				.findViewById(R.id.refunded_underline);
		// intenderfragment = new InTenderFragment();
		td_top_img = (RelativeLayout) this.findViewById(R.id.td_top_img);
		mPager = (ViewPager) findViewById(R.id.tender_detail_pager);
		mAdapter = new FragmentAdapter(getSupportFragmentManager());
		mPager.setAdapter(mAdapter);
		mPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int index) {
				// TODO Auto-generated method stub
				switch (index) {
				case 0:
					moveTab(R.id.intender);
					break;
				case 1:
					moveTab(R.id.refund);
					break;
				case 2:
					moveTab(R.id.refunded);
					break;
				}
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {

			}
		});
		// setFragmentView(intenderfragment);
	}

	public void setListener() {
		intender.setOnClickListener(this);
		refund.setOnClickListener(this);
		refunded.setOnClickListener(this);
		td_top_img.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.intender: // 投标中
			mPager.setCurrentItem(0);
			break;
		case R.id.refund: // 还款中
			mPager.setCurrentItem(1);
			break;
		case R.id.refunded:
			mPager.setCurrentItem(2);
			break;
		case R.id.td_top_img:
			finish();
			break;
		}
	}

	private void moveTab(int id) {
		switch (id) {
		case R.id.intender: // 投标中
			intender_underline.setVisibility(View.VISIBLE);
			refund_underline.setVisibility(View.INVISIBLE);
			refunded_underline.setVisibility(View.INVISIBLE);
			break;
		case R.id.refund: // 还款中
			intender_underline.setVisibility(View.INVISIBLE);
			refund_underline.setVisibility(View.VISIBLE);
			refunded_underline.setVisibility(View.INVISIBLE);
			break;
		case R.id.refunded:
			intender_underline.setVisibility(View.INVISIBLE);
			refund_underline.setVisibility(View.INVISIBLE);
			refunded_underline.setVisibility(View.VISIBLE);
			break;
		}
	}

	/**
	 * Fragment切换
	 * 
	 * @param frament
	 */
	// public void setFragmentView(Fragment frament) {
	// FragmentTransaction tran = getSupportFragmentManager()
	// .beginTransaction();
	// tran.replace(R.id.detail_frame, frament);
	// tran.commit();
	// }

	private class FragmentAdapter extends FragmentPagerAdapter {

		List<Fragment> list;

		public FragmentAdapter(FragmentManager fm) {
			super(fm);
			list = new ArrayList<Fragment>();
			list.add(TenderDetailFragment.newInstance("5002"));
			list.add(TenderDetailFragment.newInstance("5004"));
			list.add(TenderDetailFragment.newInstance("5005"));
		}

		@Override
		public Fragment getItem(int position) {
			switch (position) {
			case 0:
				return list.get(0);
			case 1:
				return list.get(1);
			case 2:
				return list.get(2);
			}
			return null;
		}

		@Override
		public int getCount() {
			return 3;
		}
	}
}
